package com.contory.ssn_lgnd.app.maingame;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import android.opengl.GLES20;
import android.util.Log;

import com.contory.ssn_lgnd.app.Assets;
import com.contory.ssn_lgnd.app.ESShaderHandleNames;
import com.contory.ssn_lgnd.app.comp.ButtonObjectPtr;
import com.contory.ssn_lgnd.app.comp.ViewTestObject;
import com.contory.ssn_lgnd.app.comp.ButtonObject;
import com.contory.ssn_lgnd.framework.Game;
import com.contory.ssn_lgnd.framework.Input.TouchEvent;
import com.contory.ssn_lgnd.framework.Pool.PoolObjectFactory;
import com.contory.ssn_lgnd.framework.gl.Camera2D;
import com.contory.ssn_lgnd.framework.gl.ESShader;
import com.contory.ssn_lgnd.framework.gl.ESShaderLoader;
import com.contory.ssn_lgnd.framework.gl.FrameBufferObject;
import com.contory.ssn_lgnd.framework.gl.SpriteBatcher;
import com.contory.ssn_lgnd.framework.impl.GLGame;
import com.contory.ssn_lgnd.framework.impl.GLScreen;
import com.contory.ssn_lgnd.framework.math.OverlapTester;
import com.contory.ssn_lgnd.framework.math.Rectangle;
import com.contory.ssn_lgnd.framework.math.Vector2;

public class MainGame extends GLScreen{
	private static final float FRUSTUM_WIDTH = 20;
	private static final float FRUSTUM_HEIGHT = 10;
	public static final float SCENE_MAX_COORDY = 30;
	
	ESShaderLoader el_vtr;
	ESShaderLoader el_frg;
	
	FrameBufferObject fb;
	ESShader defaultShader;
	ESShader circleShader;
	ESShader curtainShader;
	ESShader blurShader;
	ESShader gradShader;
	SpriteBatcher batcher;
	Camera2D cam;
	Vector2 touchPoint = new Vector2();
	
	Curtain curtain;
	Controler controler;
	Player player;
	EnemyManager enemyMG;
	TerrainPartsGroup teraGroup;
	ColisionManager cm;
	MainGameSceneObjectPtr scene;
	PlayerTraceCam ptc;
	PlayerInfoGUI infoGui;
	Fire fire;
	TextureEffectGroup tEftGroup;
	EventNotifierManager notifyMG;
	
	Random rand = new Random();
	
	ParticleEffectGroup pEftGroup;
	
	public MainGame(Game game) {
		super(game);
		fb = new FrameBufferObject(512, 215);
		el_vtr = new ESShaderLoader();
		el_frg = new ESShaderLoader();
		defaultShader = new ESShader();
		circleShader = new ESShader();
		curtainShader = new ESShader();
		blurShader = new ESShader();
		gradShader = new ESShader();
		
		batcher = new SpriteBatcher(1000, true);
	    cam = new Camera2D(GLGame.frameWidth, GLGame.frameHeight, FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
	    
	    controler = new Controler(FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
	    cm = new ColisionManager();
	    
	    curtain = new Curtain(cam);
	    
	    pEftGroup = new ParticleEffectGroup(rand);
	    
	    tEftGroup = new TextureEffectGroup();
	    
	    fire = new Fire(cm);
	    
		player = new Player(0, 0, cam, controler, cm, fire, rand, pEftGroup);
	    infoGui = new PlayerInfoGUI(2.25f, 9.0f, 4.0f, 1.5f, player, controler);
	    player.setGUIInfo(infoGui);

	    ptc = new PlayerTraceCam(cam, player);
	    
	    enemyMG = new EnemyManager(player, cm, fire, rand, pEftGroup, tEftGroup);
	    
	    teraGroup = new TerrainPartsGroup(cm);
	    
	    notifyMG = new EventNotifierManager(cm);
	    
	    scene = new MainGameSceneStage1(this);
	    scene.initialize();
	    
	    Assets.musicReady(glGame, "Stage1.mp3");
	}
	
	@Override
	public void update(float deltaTime) {
		game.getInput().getKeyEvents();
		List<TouchEvent> touchEvent = game.getInput().getTouchEvents();
		for(int i = 0; i < touchEvent.size(); i++){
			touchPoint.set(touchEvent.get(i).x, touchEvent.get(i).y);
			cam.touchToOrinWorld(touchPoint);
			scene.setTouchEvent(touchEvent.get(i), touchPoint.x, touchPoint.y);
		}
		scene.update(deltaTime);
	}

	@Override
	public void present(float deltaTime) {
		scene.draw(batcher);
	}

	@Override
	public void pause() {
		Assets.stopBGM(Assets.BGM);
		el_vtr.unload();
		el_frg.unload();
	}

	@Override
	public void resume() {
		fb.load();
		
		GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
		GLES20.glEnable(GLES20.GL_BLEND);
		GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
		
		el_vtr.load(GLES20.GL_VERTEX_SHADER, Assets.codeImporter.getShaderCode("shd_defualt_vertex.txt"));
		el_frg.load(GLES20.GL_FRAGMENT_SHADER, Assets.codeImporter.getShaderCode("shd_default_fragment.txt"));
		defaultShader.load(el_vtr.getShaderId(), el_frg.getShaderId());
		el_frg.load(GLES20.GL_FRAGMENT_SHADER, Assets.codeImporter.getShaderCode("shd_circle_fragment.txt"));
		circleShader.load(el_vtr.getShaderId(), el_frg.getShaderId());
		el_frg.load(GLES20.GL_FRAGMENT_SHADER, Assets.codeImporter.getShaderCode("shd_curtain_fragment.txt"));
		curtainShader.load(el_vtr.getShaderId(), el_frg.getShaderId());
		el_frg.load(GLES20.GL_FRAGMENT_SHADER, Assets.codeImporter.getShaderCode("shd_blur_fragment.txt"));
		blurShader.load(el_vtr.getShaderId(), el_frg.getShaderId());
		el_frg.load(GLES20.GL_FRAGMENT_SHADER, Assets.codeImporter.getShaderCode("shd_grad_fragment.txt"));
		gradShader.load(el_vtr.getShaderId(), el_frg.getShaderId());
		Assets.playBGM(Assets.BGM);
	}

	@Override
	public void dispose() {
		
	}
	
	public GLGame getGLGame(){
		return glGame;
	}
}
